// 高阶组件函数
import React from "react";
function getDisplayName(WrappedComponent) {
  return WrappedComponent.displayName || WrappedComponent.name || "Component";
}
function withMouse(WrappedComponent) {
  class Mouse extends React.Component {
    state = {
      x: 0,
      y: 0
    };
    handleMove = (e) => {
      // console.log("鼠标位置", e.clientX, e.clientY);
      this.setState({
        x: e.clientX,
        y: e.clientY,
      });
    };
    componentDidMount() {

      window.addEventListener("mousemove", this.handleMove);
    }
    componentWillUnmount() {
      window.removeEventListener("mousemove", this.handleMove);
    }
    render() {
    //   console.log("--this.props--", this.props);
      return <WrappedComponent {...this.state} {...this.props}></WrappedComponent>;
    }
  }
  //设置高阶组件名称
  Mouse.displayName = `WithMouse${getDisplayName(WrappedComponent)}`;
  return Mouse;
}
export default withMouse;
